package com.itheima.service.umsService.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.commons.PageBean;
import com.itheima.domain.db.ums.UmsResource;
import com.itheima.mapper.umsMapper.UmsResourceMapper;
import com.itheima.service.umsService.UmsResourceService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@DubboService
public class UmsResourceServiceImpl implements UmsResourceService {

    @Autowired
    private UmsResourceMapper resourceMapper;

    //添加后台资源
    @Override
    public void create(UmsResource umsResource) {
        resourceMapper.insert(umsResource);
    }

    //根据ID删除后台资源
    @Override
    public void delete(Long id) {
        resourceMapper.deleteById(id);
    }

    //分页模糊查询后台资源
    @Override
    public PageBean<UmsResource> getByCondiction(Long categoryId, String nameKeyword, Integer pageNum, Integer pageSize, String urlKeyword) {
        Page<UmsResource> page = new Page<>(pageNum, pageSize);

        LambdaQueryWrapper<UmsResource> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.like(nameKeyword!=null,UmsResource::getName,nameKeyword);
        queryWrapper.like(urlKeyword!=null,UmsResource::getUrl,urlKeyword);
        queryWrapper.eq(categoryId!=null,UmsResource::getCategoryId,categoryId);

        resourceMapper.selectPage(page,queryWrapper);

        Integer count = resourceMapper.selectCount(queryWrapper);

        page.setTotal(count);
        if (count % pageSize == 0) {
            page.setPages(count / pageSize);
        }
        if (count % pageSize != 0) {
            page.setPages((count / pageSize) + 1);
        }

        PageBean<UmsResource> pmsBrandPageBean = new PageBean<>(page);

        return pmsBrandPageBean;
    }

    //查询所有后台资源
    @Override
    public List<UmsResource> getAll() {
        List<UmsResource> resourceList = resourceMapper.selectList(null);
        System.out.println(resourceList);
        return resourceList;
    }

    //修改后台资源
    @Override
    public void update(UmsResource resource) {
        resourceMapper.updateById(resource);
    }

    //根据ID获取资源详情
    @Override
    public UmsResource getById(Long id) {
        return resourceMapper.selectById(id);
    }
}
